package com.example.safeassistan.home.index;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.UserInfoModel;
import com.example.safeassistan.base.BaseVPFragment;
import com.example.safeassistan.common.UserData;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.hiddendanger.activity.HiddendangerUnSubmit;
import com.example.safeassistan.home.activity.ActivityBacklogList;

import com.example.safeassistan.home.activity.ActivityCheckTableList;
import com.example.safeassistan.home.activity.ActivityDeviceManager;
import com.example.safeassistan.home.activity.ActivityOverDueTaskList;
import com.example.safeassistan.home.activity.CheckRecord;
import com.example.safeassistan.home.activity.FeedBack;
import com.example.safeassistan.home.activity.ImmediatelyPatrol;
import com.example.safeassistan.home.activity.OverDueHiddendanger;
import com.example.safeassistan.home.activity.RiskPointDetail;
import com.example.safeassistan.home.activity.RiskPointList;
import com.example.safeassistan.home.activity.TemporaryTask;
import com.example.safeassistan.home.adapter.RiskDetailRiskFragmentPAdapter;
import com.example.safeassistan.home.model.HomeBanner;
import com.example.safeassistan.home.model.IndedxDataModel;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.message.activity.Activity_Message;
import com.example.safeassistan.risk.model.RiskPointDetailRisk;
import com.example.safeassistan.utile.JsonUtils;
import com.example.safeassistan.utile.PictureUtils;
import com.stx.xhb.xbanner.XBanner;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class HomeIndex extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private View view;
    private LinearLayout llbacklog,llOverDueHiddenDanger,llFeedback,
            llOverDuetask,llImmediatelyPatrol
            ,llTemporaryTask,llCheckRecord,llRiskPoint,llDeviceManager;
    private ImageView ivMsg;
    private XBanner banner;
    private TextView tvWaitTask,tvOverDueTask,tvOverDueHiddenDanger;
    private TextView tvCompanyName;
    private ImageView ivCompanyImg;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.llbacklog:
                //待办任务
                ActivityBacklogList.actionStart(context);
                break;
            case R.id.ivMsg:
                Activity_Message.actionStart(context);
                break;
            case R.id.llOverDueHiddenDanger:
                //逾期隐患
                OverDueHiddendanger.actionStart(context);
                break;
            case R.id.llFeedback:
                //随手拍
                FeedBack.actionStart(context);
                break;
            case R.id.llOverDuetask:
                //逾期任务
                ActivityOverDueTaskList.actionStart(context);
                break;
            case R.id.llImmediatelyPatrol:
                //立即巡查
                ImmediatelyPatrol.actionStart(context);
                break;
            case R.id.llTemporaryTask:
                //临时任务
                TemporaryTask.actionStart(context);
                break;
            case R.id.llCheckRecord:
                //检查记录
                CheckRecord.actionStart(context);
                break;
            case R.id.llRiskPoint:
                //风险点查询
                RiskPointList.actionStart(context,1);
                break;
            case R.id.llDeviceManager:
                //设备管理
                ActivityDeviceManager.actionStart(context);
                break;
        }
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible){
            getData();
            getTopData();
            Glide.with(context).load(UserData.create(context).get(UserData.companyImg))
                    .placeholder(R.mipmap.icon_headlink)
                    .apply(new RequestOptions().transform(new PictureUtils()))
                    .into(ivCompanyImg);
            tvCompanyName.setText(UserData.create(context).get(UserData.companyName)==null?"未知":UserData.create(context).get(UserData.companyName));
        }
    }

    @Override
    protected void onFragmentFirstVisible() {

    }

    public static HomeIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
        HomeIndex fl = new HomeIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.home_index, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        llbacklog=view.findViewById(R.id.llbacklog);
        llbacklog.setOnClickListener(this::onClick);
        ivMsg=view.findViewById(R.id.ivMsg);
        tvCompanyName=view.findViewById(R.id.tvCompanyName);
        ivCompanyImg=view.findViewById(R.id.ivCompanyLogo);
        tvOverDueTask=view.findViewById(R.id.tvOverDueTask);
        tvWaitTask=view.findViewById(R.id.tvWaitTask);
        tvOverDueHiddenDanger=view.findViewById(R.id.tvOverDueHiddenDanger);
        ivMsg.setOnClickListener(this::onClick);
        llOverDueHiddenDanger=view.findViewById(R.id.llOverDueHiddenDanger);
        llOverDueHiddenDanger.setOnClickListener(this::onClick);
        llFeedback=view.findViewById(R.id.llFeedback);
        llFeedback.setOnClickListener(this::onClick);
        llOverDuetask=view.findViewById(R.id.llOverDuetask);
        llOverDuetask.setOnClickListener(this::onClick);
        llImmediatelyPatrol=view.findViewById(R.id.llImmediatelyPatrol);
        llImmediatelyPatrol.setOnClickListener(this::onClick);
        llTemporaryTask=view.findViewById(R.id.llTemporaryTask);
        llTemporaryTask.setOnClickListener(this::onClick);
        llCheckRecord=view.findViewById(R.id.llCheckRecord);
        llCheckRecord.setOnClickListener(this::onClick);
        llRiskPoint=view.findViewById(R.id.llRiskPoint);
        llRiskPoint.setOnClickListener(this::onClick);
        llDeviceManager=view.findViewById(R.id.llDeviceManager);
        llDeviceManager.setOnClickListener(this::onClick);
        banner=view.findViewById(R.id.banner);
    }


    public void getData(){
        new HttpRequest(context).doGet(UrlConstants.homebanner, null, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean  success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), HomeBanner.class);
                        if (obj instanceof HomeBanner) {
                            HomeBanner  homeBanner=(HomeBanner) obj;
                            if (homeBanner.getResult().length()>0){
                                String [] strs=homeBanner.getResult().split(",");
                                List banners=new ArrayList();
                                for (int i=0;i<strs.length;i++){
                                    banners.add(strs[i]);
                                }
                                banner.setVisibility(View.VISIBLE);
                                banner.setData(R.layout.item_banner,banners,null);
                                banner.loadImage(new XBanner.XBannerAdapter() {
                                    @Override
                                    public void loadBanner(XBanner banner, Object object, View view, int position) {
                                        RelativeLayout container=(RelativeLayout) view;
                                        ImageView ivBanner=container.findViewById(R.id.ivBanner);
                                        Glide.with(context).load(banners.get(position))
                                                .placeholder(R.mipmap.icon_home_banner)
                                                .into(ivBanner);
                                    }
                                });
                            }
                        }
                    }else {
                        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }

    public void getTopData(){
        new HttpRequest(context).doGet(UrlConstants.task_queryTotal, null, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean  success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), IndedxDataModel.class);
                        if (obj instanceof IndedxDataModel) {
                            IndedxDataModel model=(IndedxDataModel)obj;
                            setView(model);
                        }
                    }else {
                        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) { }
        });
    }

    public void setView(IndedxDataModel model){
        String [] s1=model.getResult().get(0).split(":");
        if (s1.length>1){
            tvWaitTask.setText(s1[1]);
        }
        String [] s2=model.getResult().get(1).split(":");
        if (s2.length>1){
            tvOverDueTask.setText(s2[1]);
        }
        String [] s3=model.getResult().get(2).split(":");
        if (s3.length>1){
            tvOverDueHiddenDanger.setText(s3[1]);
        }
    }
}